package message.visitor.tests;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import junit.framework.Assert;
import message.MessageParser;
import message.types.data.AddDataRequest;
import message.types.data.GetAllDatasAnswer;
import message.types.data.GetLastDataAnswer;
import message.types.message.RejectedRequest;
import message.types.none.GetAllDatasRequest;
import message.types.none.GetLastDataRequest;
import message.types.none.NewDataNotification;
import message.types.stalker.LogOffNotification;
import message.types.stalker.RegisterRequest;
import message.types.stalkerstring.GetRoomFromNameAnswer;
import message.types.stalkerstring.GetUserFromNameAnswer;
import message.types.string.CreatePrivatePersistentRoomRequest;
import message.types.string.CreateRoomRequest;
import message.types.string.GetRoomFromNameRequest;
import message.types.string.GetUserFromNameRequest;
import message.types.unique.EmptyMessage;
import message.types.unique.RoomInvitation;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import network.link.FrameInputStream;
import network.link.FrameOutputStream;

import org.junit.Test;

import stalker.Stalker;
import stalker.tester.TesterStalker;
import stalker.user.User;
import data.Data;
import data.content.Content;
import data.content.ContentSentence;
import data.datas.CompleteHistoryDatas;
import data.datas.Datas;


public class ClientMessageVisitorTest {
  
  @Test
  public void test01() throws InterruptedException, IOException {
    
    
    FrameReader tread, reader;
    FrameWriter writer;
    tread = new FrameReader(new FrameInputStream(System.in));
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    writer = new FrameWriter(new FrameOutputStream(bytes));
    
    User test_usr = User.createUser("Yunis");
    TesterStalker tester = new TesterStalker();
    
    Stalker.SERVER = false;
    
    //writer simulates net
    ClientMessageVisitorTestServiceRequestCenterStub src;
    src = new ClientMessageVisitorTestServiceRequestCenterStub(
        test_usr,
        tread,
        writer);
    
    Content test_content = new ContentSentence("Love");
    Data test_data = new Data(test_usr, test_content);
    Datas test_datas = new CompleteHistoryDatas();
    test_datas.insertData(test_data);
    
    src.acceptMessage(
        new AddDataRequest(test_usr, tester, test_content)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetLastDataRequest(test_usr, tester)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetLastDataAnswer(tester, test_usr, test_data)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new RegisterRequest(test_usr, tester, test_usr)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new EmptyMessage()
        );
    Thread.sleep(25);
    src.acceptMessage(
        new CreateRoomRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Love")
        );
    Thread.sleep(25);
    src.acceptMessage(
        new CreatePrivatePersistentRoomRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Love")
        );
    Thread.sleep(25);
    src.acceptMessage(
        new RoomInvitation(tester, test_usr, tester, test_usr)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetAllDatasRequest(test_usr, tester)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetAllDatasAnswer(tester, test_usr, test_datas)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetUserFromNameRequest(test_usr, Stalker.USER_MANAGER, "Kya")
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetUserFromNameAnswer(Stalker.USER_MANAGER, test_usr, test_usr, "Kya")
        );
    Thread.sleep(25);
    src.acceptMessage(
        new NewDataNotification(tester, test_usr)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new LogOffNotification(test_usr, tester, test_usr)
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetRoomFromNameRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Kya")
        );
    Thread.sleep(25);
    src.acceptMessage(
        new GetRoomFromNameAnswer(Stalker.ROOM_MANAGER_STALKER, test_usr, test_usr, "Kya")
        );
    Thread.sleep(25);
    
    src.acceptMessage(
        new RejectedRequest(tester, test_usr, new EmptyMessage())
        );
    Thread.sleep(25);
    
    src.acceptMessage(
        new RejectedRequest(test_usr, tester, new EmptyMessage())
        );
    Thread.sleep(25);
    
    reader = new FrameReader(new FrameInputStream(new ByteArrayInputStream(bytes.toByteArray())));
    
    System.out.println(new String(bytes.toByteArray()));
    
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new AddDataRequest(test_usr, tester, test_content)
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new GetLastDataRequest(test_usr, tester)
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new RegisterRequest(test_usr, tester, test_usr)
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new CreateRoomRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Love")
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new CreatePrivatePersistentRoomRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Love")
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new GetAllDatasRequest(test_usr, tester)
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new GetUserFromNameRequest(test_usr, Stalker.USER_MANAGER, "Kya")
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new LogOffNotification(test_usr, tester, test_usr)
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new GetRoomFromNameRequest(test_usr, Stalker.ROOM_MANAGER_STALKER, "Kya")
        ));
    Assert.assertTrue(MessageParser.retrieve(reader).equals(
        new RejectedRequest(test_usr, tester, new EmptyMessage())
        ));
    
  }
  
}
